package com.keyboardr.gamerater.task;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.json.JSONArray;

import com.keyboardr.gamerater.model.Group;

import android.os.AsyncTask;

public class SaveFileTask extends AsyncTask<Group, Void, File> {

	private final String mPath;

	public SaveFileTask(String filePath) {
		mPath = filePath;
	}

	@Override
	protected File doInBackground(Group... saveGroups) {
		File file = null;
		try {
			file = new File(mPath);
			file.createNewFile();

			JSONArray jArray = new JSONArray();
			for (Group group : saveGroups) {
				jArray.put(group.toJSON());
			}

			FileWriter writer = new FileWriter(file, false);
			writer.append(jArray.toString());

			writer.flush();
			writer.close();
			return file;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return file;
	}

}
